//
//  HMSportMapViewController.m
//  黑马行
//
//  Created by 宇宙执政 on 16/9/20.
//  Copyright © 2016年 yujianbinMAC. All rights reserved.
//

#import "HMSportMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import "HMSportTrackingLine.h"
#import "HMCircleAnimator.h"

@interface HMSportMapViewController ()<MAMapViewDelegate>
{
    BOOL _isStartLocation;
    HMCircleAnimator *_animator;
}

@end

@implementation HMSportMapViewController

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super initWithCoder:aDecoder]) {
        
        self.modalPresentationStyle = UIModalPresentationCustom;
        
        _animator = [HMCircleAnimator new];
        
        self.transitioningDelegate = _animator;
        
    }
    
    return self;
    
}

-(void)awakeFromNib{
    [super awakeFromNib];
    
    [self setupMapView];

}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark MapViewDelegate
/**
 * @brief 位置或者设备方向更新后，会调用此函数
 * @param mapView 地图View
 * @param userLocation 用户定位信息(包括位置与设备方向等数据)
 * @param updatingLocation 标示是否是location数据更新, YES:location数据更新 NO:heading数据更新
 */
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    
    //0,判断位置数据变化，不一定是经纬度变化，大概1s更新一次
    if (!userLocation) {
        return;
    }
    
    //1,判断运动状态，只有'继续'才需要绘制运动轨迹
    if (_sportTracking.sportState != HMSportStateContinue) {
        
        return;
    }
    
    //将用户位置设置在地图中心点
//    [mapView setCenterCoordinate:userLocation.coordinate];
    
    
    if (!_isStartLocation && _sportTracking.startLocation != nil) {
        
        _isStartLocation = NO;
        
        //1,实例化大头针
        MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
        
        //2,指定坐标位置
        annotation.coordinate = _sportTracking.startLocation.coordinate;
        
        //3,添加到地图视图
        [mapView addAnnotation:annotation];
    }
    
    //2,绘制运动轨迹
    [mapView addOverlay:[_sportTracking appendLocation:userLocation.location]];
}

/**
 * @brief 根据anntation生成对应的View
 * @param mapView 地图View
 * @param annotation 指定的标注
 * @return 生成的标注View
 */
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    
    //1,判断大头针类型,
    if (![annotation isKindOfClass:[MAPointAnnotation class]]) {
        return nil;//表明是系统的大头针
    }
    
    //2,查询可重用大头针
    static NSString *annotationId = @"annotationid";
    MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:annotationId];
    
    //3,如果没有，就创建
    if (!annotationView) {
        annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationId];
    }
    
    //4,设置大头针图像 - 设置图像
    //根据运动类型来创建运动图像
    UIImage *image = _sportTracking.sportImage;
    annotationView.image = image;
    annotationView.centerOffset = CGPointMake(0, -image.size.height * 0.5);
    
    
    //5，返回自定义大头针
    return annotationView;
}

/**
 * @brief 单击地图回调，返回经纬度
 * @param mapView 地图View
 * @param coordinate 经纬度
 */
//主要是测试画折线
- (void)mapView:(MAMapView *)mapView didSingleTappedAtCoordinate:(CLLocationCoordinate2D)coordinate{
    
//    NSLog(@"%f, %f",coordinate.latitude, coordinate.longitude);
    
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    //创建轨迹追踪线条模型
//    CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
//    
//    HMSportTrackingLine *trackingLine = [[HMSportTrackingLine alloc] initWithStartLocation:_startLocation endLocation:endLocation];
//    
//    //3,添加到地图
//    [mapView addOverlay:trackingLine.polyline];
    
    
}

/**
 * @brief 根据overlay生成对应的Renderer
 * @param mapView 地图View
 * @param overlay 指定的overlay
 * @return 生成的覆盖物Renderer
 */
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id<MAOverlay>)overlay{
    
    //1,判断 overlay的类型
    if (![overlay isKindOfClass:[MAPolyline class]]) {
        return  nil;
    }
    
    HMSportPolyline *polyLine = (HMSportPolyline *)overlay;
    
    //2,实例化折线渲染器
    MAPolylineRenderer *renderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
    
    //3,设置显示属性
    renderer.lineWidth = 5;
    renderer.strokeColor = polyLine.color;
    
    return renderer;
}

////实例化地图
-(void)setupMapView{
    
    //1，实例化地图控件
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    
    //2,将地图添加到根视图
    [self.view insertSubview:mapView atIndex:0];
    
    //3,隐藏比例尺
    mapView.showsScale = NO;
    
    //4,关闭相机旋转  - 能够降低能耗，省电
    mapView.rotateCameraEnabled = NO;//默认为yes
    
    //5,显示用户位置
    mapView.showsUserLocation = YES;
    
    //6,跟踪用户位置 - 追踪用户的location与heading更新/会放大地图显示，将用户设置在地图的中心点
    mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;
    
    //7，允许后台定位 - 保证background modes中的location update处于选中状态
    mapView.allowsBackgroundLocationUpdates = YES;
    
    //8，不允许系统暂停位置更新
    mapView.pausesLocationUpdatesAutomatically = NO;
    
    //9,设置代理
    mapView.delegate = self;
    
    //10，记录mapView
    _mapView = mapView;
    
}

@end
